package com.credit.management.system.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.Page;
import com.credit.management.system.model.Syuser;
import com.credit.management.system.model.Syuserrole;
import com.credit.management.system.service.ISyuserService;
import com.credit.management.system.service.ISyuserroleService;
@RequestMapping(value="/syuser")
@Controller
public class SyuserController {
	@Autowired
	private ISyuserService syuserService;
	@Autowired
	private ISyuserroleService syuserroleService;
	/**
	 * 进入syuser用户主页面
	 * @return
	 */
	@RequestMapping(value="/syuser",method=RequestMethod.GET)
	public String syuser(){
		return "syauthority/syuser";
	}
	/**
	 * 分页查询syusers，给datatables返回值
	 * @param start
	 * @param length
	 * @param page 
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(@RequestParam(value="order[0][column]")String a,Integer start, Integer length, Object total)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		System.out.println("a:"+a);
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		page=syuserService.findPageSyusers(page);
		
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	/**
	 * 添加syuser
	 * @param syuser
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Integer add(	Syuser syuser){
		Integer result=syuserService.insertSelective(syuser);
		return result;
	}
	/**
	 * 通过id拿到修改的数据填充修改弹框
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/updatePre",method=RequestMethod.POST)
	@ResponseBody
	public Syuser updatePre(Integer id){
		Syuser syuser=syuserService.selectByPrimaryKey(id);
		return syuser;
	}
	/**
	 * 修改syuser
	 * @param syuser
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Integer update(Syuser syuser){
		Integer result=syuserService.updateByPrimaryKeySelective(syuser);
		return result;
	}
	/**
	 * 根据id删除syuser
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Integer delete(Integer id){
		Integer result=0;
		
		result=syuserService.deleteByPrimaryKey(id);
		return result;
	}
	
	/**
	 * 查询该用户所具有的角色和不具有的角色
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/grant",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>> grant(Integer id){
		List<Map<String,Object>> maps=syuserService.findUserAndRole(id);
		return maps;
	}
	/**
	 * 1、通过用户id删除该用户的所有角色
	 * 2、在将checkbox上被勾选的角色插入到 user-role表中
	 * @param id
	 * @param roleIds
	 * @return
	 */
	@RequestMapping(value="/grantRole",method=RequestMethod.POST)
	@ResponseBody
	public Integer grantRole(Integer id,Integer[] roleIds){
		System.out.println("id:"+id);
		syuserroleService.deleteUserRoles(id);
		if(roleIds!=null &&roleIds.length>0){
			
			for(int i=0;i<roleIds.length;i++){
				Syuserrole syuserrole=new Syuserrole ();
				syuserrole.setRoleid(roleIds[i]);
				syuserrole.setUserid(id);
				System.out.println("roleIds[i]:"+roleIds[i]);
				syuserroleService.insertSelective(syuserrole);
			}
		}
		return 0;
	}
}
